package com.ubs.training.server.aop;

import com.ubs.training.prod.server.service.LoggingService;
import com.ubs.training.server.dao.model.Contract;
import com.ubs.training.server.service.ContractService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/24/12
 * Time: 5:11 AM
 * To change this template use File | Settings | File Templates.
 */

@Aspect
@Component
public class ContractLoggingAdvice extends AbstractLoggingAdvice {

    @Resource(name="loggingService")
    private LoggingService svc;

    public LoggingService getSvc() {
        return svc;
    }

    public void setSvc(LoggingService svc) {
        this.svc = svc;
    }


    @Around("execution(* com.ubs.training.server.service.ContractService.*(..))")
    public Object processAllMethods(ProceedingJoinPoint pjp) throws Throwable {
        String targetName = pjp.getTarget().getClass().getSimpleName();
        String methodName = pjp.getSignature().getName();
        Object obj;
        if (methodName.equals("getByClientId")){
            svc.logEvent("User got all contracts.");
        }
        else if (methodName.equals("getById")){
            svc.logEvent("User got contract by id.");
        }
        else if (methodName.equals("save")){
            if (((ContractService)pjp.getTarget()).isNew((Contract)pjp.getArgs()[0])){
                svc.logEvent("User added contract.");
            }
            else {
                svc.logEvent("User updated contract.");
            }
        }
        else if (methodName.equals("delete")){
            svc.logEvent("User deleted contract.");
      }
        obj=pjp.proceed();
        return obj;
    }
}